package com.test.util;

import android.util.Log;

import com.test.R;

import java.lang.reflect.Field;

/**
 * 获取图片id工具类
 * @Author Musique
 * 2020/8/5
 */
public class ImgUtil {

    /**
     * 获取图片名称获取图片的资源id的方法
     * (mipmap)
     */
    public static int getResourceByReflect(String imageName) {
        Class mipmap = R.mipmap.class;
        // Class mipmaps = mipmap;
        Field field = null;
        int r_id = 0;
        try {
            field = mipmap.getField(imageName);
            field.setAccessible(true);
            r_id = field.getInt(field.getName());
        } catch (Exception e) {
            Log.e("ERROR", "PICTURE NOT　FOUND！");
        }
        return r_id;
    }

    /**
     * 获取图片名称获取图片的drawable id的方法
     */
    public static int getDrawableByReflect(String imageName, Class drawable) {
        //        Class drawable = R.drawable.class;
        Field field = null;
        int r_id = 0;
        try {
            field = drawable.getField(imageName);
            field.setAccessible(true);
            r_id = field.getInt(field.getName());
        } catch (Exception e) {
            Log.e("ERROR", "PICTURE NOT　FOUND！");
        }
        return r_id;
    }
}
